from unittest import result
import requests
from bs4 import BeautifulSoup
import re
import sys

# if len(sys.argv) > 1:
#     url = sys.argv[1]
# else:
#     sys.exit("Error: Please enter the TED Talk URL")

url = 'https://www.ted.com/talks/karen_lellouche_tordjman_siri_alexa_google_what_comes_next'

r = requests.get(url)

print('Download about to start')

soup = BeautifulSoup(r.content)

for val in soup.findAll('script'):
    if re.search('__NEXT_DATA__', str(val)) is not None:
        result = str(val)

result_mp4 = re.search("(?P<url>https?://[\S]+mp4)", result).group("url")
mp4_url = result_mp4.split('"')[0]

print('Downloading video from ......'+mp4_url)

file_name = mp4_url.split("/")[len(mp4_url.split("/"))-1].split('?')[0]

r = requests.get(mp4_url)

with open(file_name, 'wb') as f:
    f.write(r.content)

print('Download Process Finished!')
